<?php

class Cops_model extends model{
	function __construct(){
		parent::model();
	}
	
	function add_cops(){
		$group_name = $this->input->post('cops_entry_name');
		$description = $this->input->post('content_2');
		$description_r = htmlspecialchars($description);
		$date_format = "%Y-%m-%d %h:%i:%a";
		$cops_create_date = mdate($date_format);
		
		$data = array(
			'group_name' => $group_name,
			'description' => $description_r,
			'owner' => $this->session->userdata('member_id'),
			'create_date' => $cops_create_date
		);
		
		$this->db->insert('cops_group',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function upload_logo($data){
		$file_name =  $data['raw_name']."_thumb".$data['file_ext'];
		
		$update_logo = array('logo' => $file_name);
		
		$member_id = $this->session->userdata('member_id');
		
		
		$this->db->where('group_id',$data['group_id']);
		
		$this->db->update('cops_group',$update_logo);
		
		return true;
	}
	
	function add_member_to_group($id){
		$group_id = $id;
		$data = array(
			'member_id' => $this->session->userdata('member_id'),
			'group_id' => $group_id['id'] 
		);
		
		$this->db->insert('cops_group_in',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function leave_group_by_id($r){
		$this->db->where('group_id',$r['group_id']);
		$this->db->where('member_id',$r['member_id']);
		$this->db->delete('cops_group_in');
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function delete_group_by_id($r){
		$this->db->where('group_id',$r['group_id']);
		$this->db->delete('cops_group');
		$query_1 = $this->db->affected_rows();
		
		$this->db->where('group_id',$r['group_id']);
		$this->db->delete('cops_group_in');
		$query_2 = $this->db->affected_rows();
		
		if(($query_1&&$query_2) || ($query_1&&$query_2==false)){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function discuss_post(){
		$member_id = $this->input->post('member_id');
		$group_id = $this->input->post('group_id');
		
		$content = $this->input->post('content');
		$content_r = htmlspecialchars($content);
		$date_format = "%Y-%m-%d %h:%i:%a";
		$post_date = mdate($date_format);
		
		if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
			 {
				$ip=$_SERVER['HTTP_CLIENT_IP'];
			 }
			 else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
			 {
				$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
			 }
			 else
			 {
				$ip=$_SERVER['REMOTE_ADDR'];
			 }
		
		$data = array(
			'content' => $content,
			'member_id' => $member_id,
			'group_id' => $group_id,
			'date' => $post_date,
			'ip_address' => $ip,
			'browser_info' => $_SERVER['HTTP_USER_AGENT']
		);
		
		$this->db->insert('cops_discuss',$data);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function discuss_del($r){
		$this->db->where('discuss_id',$r['discuss_id']);
		$this->db->delete('cops_discuss');
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function insert_files($data){
		$group_id = $this->input->post('group_id');
		$member_id = $this->session->userdata('member_id');
		$date_format = "%Y-%m-%d %h:%i:%a";
		$date_upload = mdate($date_format);
		
		$set_ins = array(
			'files_name' => $data['file_name'],
			'description' => $this->input->post('description'),
			'orig_name' => $data['orig_name'],
			'file_ext' => $data['file_ext'],
			'file_size' => $data['file_size'],
			'file_type' => $data['file_type'],
			'date_upload' => $date_upload,
			'member_id' => $member_id,
			'group_id' => $group_id
		);
		
		$this->db->where('group_id',$group_id);
		$this->db->insert('cops_files',$set_ins);
		
		$query = $this->db->affected_rows();
		
		if($query){
			return true;
		}
		else
		{
			return false;
		}
	}
}